<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .bg {
            background-color: black;
        }
    </style>
</head>

<body>
    <div class="one"></div>
    <button>开关</button>
    <script>
        var div = document.querySelector('div')
        //1. 获取元素相应索引的类名 element.classlist[] 
        // console.log(div.classList[0]);
        // 2.添加元素类 element.classList.add('')//参数类名前皆不加点 为字符型 且不覆盖原有的类
        div.classList.add('two')
        // console.log(div);
        // 3.删除元素类名 element.classList.remove('')
        div.classList.remove('two')
        console.log(div);
        // 4.切换元素类名 element.classList.toggle('')
        var btn = document.querySelector('button')
        btn.addEventListener('click', function () {
            document.body.classList.toggle('bg')
        })


    </script>
</body>

</html>